package other;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import net.miginfocom.swing.MigLayout;
import view.LoginPanel;

/**
 * Represents the login dialog box.
 *	
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class LoginDialog extends JDialog
{
	// Variables.
	private final String title = "Welcome! Please Login!";
	
	private JLabel userNameLabel;
	private JLabel userPassLabel;
	
	private JTextField userNameField;
	private JPasswordField userPassField; 

	private JButton submitButton;
	private JButton helpButton;
	private JPanel buttonPanel;
	
	private LoginPanel parentPanel;
	
	// Custom Layout.
	private MigLayout migLayout;
	
	/**
	 * Constructs the login dialog.
	 */
	public LoginDialog(LoginPanel panel) 
	{
		// Initialize values.
		parentPanel = panel;
		
		// Initialize dialog.
		this.init();
	}
	
	/**
	 * Initialize the login dialog GUI.
	 */
	public void init()
	{
		// Set title.
		this.setTitle(title);
		
		// Set layout.
		migLayout = new MigLayout("", "[right]");
		this.setLayout(migLayout);
		
		// Set labels.
		userNameLabel = new JLabel("User Name:");
		userPassLabel = new JLabel("Password:");
		
		// Set text fields.
		userNameField = new JTextField(15);
		userPassField = new JPasswordField(15);
		
		// Set buttons.
		submitButton = new JButton("Log In");
		helpButton = new JButton("Help");
		buttonPanel = new JPanel(new FlowLayout());
		
		// Add components.
		this.add(userNameLabel, "gap 10");
		this.add(userNameField, "span, growx");
		
		this.add(userPassLabel, "gap 10");
		this.add(userPassField, "span, growx, wrap");
		
		buttonPanel.add(submitButton);
		buttonPanel.add(helpButton);
		
		this.add(buttonPanel, "span, center");
	
		this.pack();
		this.setLocationRelativeTo(null); // Center it.
		this.setResizable(false);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.setAlwaysOnTop(true);
	}
	
	/**
	 * Enable/Disable the dialog's visibility.
	 * @param value
	 */
	public void setLoginDialogVisible(boolean value)
	{
		this.setVisible(value);
	}
	
	// Listeners.
	public void setSubmitListener(ActionListener e)
	{
		submitButton.addActionListener(e);
	}
	
	public void setHelpListener(ActionListener e)
	{
		helpButton.addActionListener(e);
	}
	
	// Please feel free to incorporate any other methods as required.
}
